<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="文件名" prop="fileName">
        <el-input v-model="dataForm.fileName" placeholder="文件名"></el-input>
      </el-form-item>
      <el-form-item label="用户名" prop="用户名">
        <el-tag>{{
          dataForm.user.name == null || dataForm.user.name == ""
            ? "无"
            : dataForm.user.name
        }}</el-tag>
      </el-form-item>
      <el-form-item label="订单号" prop="uuid">
        <el-tag>{{ dataForm.uuid }}</el-tag>
      </el-form-item>
      <el-form-item label="文件链接地址" prop="link">
        <el-input v-model="dataForm.link" placeholder="文件链接地址"></el-input>
      </el-form-item>
      <el-form-item label="分页页数" prop="pages">
        <el-input v-model="dataForm.pages" placeholder="分页页数"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        pages: 0,
        deleted: "",
        fileName: "",
        userId: "",
        uuid: "",
        link: "",
        id: "",
        user: [],
      },
      dataRule: {
        deleted: [
          { required: true, message: "逻辑删除不能为空", trigger: "blur" },
        ],
        fileName: [
          { required: true, message: "文件名不能为空", trigger: "blur" },
        ],
        userId: [
          { required: true, message: "用户id不能为空", trigger: "blur" },
        ],
        uuid: [{ required: true, message: "订单号不能为空", trigger: "blur" }],
        link: [
          { required: true, message: "文件链接地址不能为空", trigger: "blur" },
        ],
        id: [{ required: true, message: "id主键不能为空", trigger: "blur" }],
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/help/files/info/${this.dataForm.id}`),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.deleted = data.fileS.deleted;
              this.dataForm.fileName = data.fileS.fileName;
              this.dataForm.userId = data.fileS.userId;
              this.dataForm.uuid = data.fileS.uuid;
              this.dataForm.link = data.fileS.link;
              this.dataForm.id = data.fileS.id;
              this.dataForm.user = data.fileS.user;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/help/files/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              pages: this.dataForm.pages || undefined,
              deleted: this.dataForm.deleted,
              fileName: this.dataForm.fileName,
              userId: this.dataForm.userId,
              uuid: this.dataForm.uuid,
              link: this.dataForm.link,
              id: this.dataForm.id,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
<style>
.CSS {
  background-color: rgb(42, 85, 226);
}
</style>